import Index from '@/views/Index.vue'
import Login from '@/views/Login.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { useCookies } from '@vueuse/integrations/useCookies'
import http from '@/http'
import { useUserStore } from '@/stores/user'
import User from '@/views/User.vue'
import NotFound from '@/views/404.vue'
import Role from '@/views/Role.vue'
import Sales from '@/views/Sales.vue'
import SalesZong from '@/views/SalesZong.vue'
import Sales2 from '@/views/Sales2.vue'
import CheckZong from '@/views/CheckZong.vue'
import Check1 from '@/views/Check1.vue'
import Check2 from '@/views/Check2.vue'
import Workload from '@/views/Workload.vue'
import Workload1 from '@/views/Workload1.vue'
import Workload2 from '@/views/Workload2.vue'
import InsFee from '@/views/InsFee.vue'

import Menu from "@/views/Menu.vue";
import Registered from "@/views/Registered.vue";
import Department from "@/views/Department.vue";
import RegisteredList from "@/views/RegisteredList.vue";
import NewCare from "@/views/NewCare.vue";
import RegFee from '@/views/RegFee.vue'
import Producter from '@/views/Producter.vue'
import Medicinal from '@/views/Medicinal.vue'
import Dict from "@/views/Dict.vue";
import Notice from "@/views/Notice.vue";
import Operate from "@/views/Operate.vue";
import LoginLog from "@/views/LoginLog.vue";
import Docharge from '@/views/Docharge.vue'
import Dispensing from '@/views/Dispensing.vue'
import Docheck from '@/views/Docheck.vue'
import Checkresult from '@/views/Checkresult.vue'
import Checklist from '@/views/Checklist.vue'
import Scheduling from '@/views/Scheduling.vue'
import Myscheduling from '@/views/Myscheduling.vue'
import Chargelist from '@/views/Chargelist.vue'
import Backfee from '@/views/Backfee.vue'
import Backfeelist from '@/views/Backfeelist.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/index",
    },
    {
      path: "/index",
      name: "index",
      component: Index,
      //子组件路由
      children: [
        {
          path: "system/user",
          name: "user",
          component: User,
        },
        {
          path: "system/role",
          name: "role",
          component: Role,
        },
        {

          path: 'statistics/sales',
          name: 'sales',
          component: SalesZong,
          children: [
            {
              path: "sales1",
              name: "sales1",
              component: Sales,
            },
            {
              path: "sales2",
              name: "sales2",
              component: Sales2,
            },
          ],
        },
        {
          path: "statistics/check",
          name: "check",
          component: CheckZong,
          children: [
            {
              path: "Check1",
              name: "Check1",
              component: Check1,
            },
            {
              path: "Check2",
              name: "Check2",
              component: Check2,
            },
          ],
        },
        {
          path: "statistics/workload",
          name: "workload",
          component: Workload,
          children: [
            {
              path: "workload1",
              name: "workload1",
              component: Workload1,
            },
            {
              path: "workload2",
              name: "workload2",
              component: Workload2,
            },
          ],
        },
        {
          path:'system/ins_fee',
          name: 'ins_fee',
          component : InsFee
        },
        {
          path: 'system/menu',
          name: 'menu',
          component: Menu
        },
        {
          path: 'system/dept',
          name: 'dept',
          component: Department
        },
        {
          path: 'system/reg_fee',
          name: 'reg_fee',
          component:RegFee
        },
        {
          path: 'stock/producter',
          name: 'producter',
          component:Producter
        },
        {
          path: 'stock/medicinal',
          name: 'medicinal',
          component:Medicinal
        },
        {
          path: "system/notice",
          name: "notice",
          component: Notice,
        },
        {
          path: "system/dict",
          name: "dict",
          component: Dict,
        },
        {
          path: "system/log_login",
          name: "log_login",
          component: LoginLog,
        },
        {
          path: "system/log_opt",
          name: "log_opt",
          component: Operate,
        },
        {
          path: "doctor/registered",
          name: "registered",
          component: Registered,
        },
        {
          path: "doctor/registeredlist",
          name: "registeredlist",
          component: RegisteredList,
        },
        {
          path: "doctor/newcare",
          name: "newcare",
          component: NewCare,
        },
        {
          path: "doctor/scheduling",
          name: "scheduling",
          component: Scheduling,
        },
        {
          path: "charge/docharge",
          name: "docharge",
          component: Docharge,
        },
        {
          path: "charge/dispensing",
          name: "dispensing",
          component: Dispensing,
        },
        {
          path: "charge/chargelist",
          name: "chargelist",
          component: Chargelist,
        },
        {
          path: "charge/backfee",
          name: "backfee",
          component: Backfee,
        },
        {
          path: "charge/backfeelist",
          name: "backfeelist",
          component: Backfeelist,
        },
        {
          path: "check/docheck",
          name: "docheck",
          component: Docheck,
        },
        {
          path: "check/checkresult",
          name: "checkresult",
          component: Checkresult,
        },
        {
          path: "check/checklist",
          name: "checklist",
          component: Checklist,
        },

        {
          path: "doctor/myscheduling",
          name: "myscheduling",
          component: Myscheduling
        },
        //没有路由匹配404组件
        {
          path: "/:pathMatch(.*)*",
          name: "404",
          component: NotFound,
        }
      ],
    },

    {
      path: "/login",
      name: "login",
      component: Login,
    },
  ],
});

//路由前置守卫，所有的路由匹配组件显示之前进入前置守卫
router.beforeEach(async (to, from) => {
  const cookies = useCookies();
  //从cookie中获取token
  const token = cookies.get("token");
  //console.log(uid)
  //如果没有登录，并且去的也不是登录。说明不能去，跳转登录先进行登录
  if (token == undefined && to.path != "/login") {
    return "/login";
  }
  //如果已经登录,先根据查询用户对象，访问目标组件
  if (token != undefined) {
    //向后端获取用户对象
    await http.get("/user/getUser").then((res) => {
      console.log(res);
      //将用户对象存储到pinia的用户数据单元
      const userStore = useUserStore();
      userStore.setUser(res);
    });
    //进入目标组件
  }
});

export default router;
